package com.google.code.sysarch.v0.e35;

import com.google.code.sysarch.common.util.GraphicBufferThreadA1;

class iThread_35 extends Thread {

	final static String[] threadNames = { 
		"Albert", "Leonardo", "Galileo", "Adam", "Linus", "Bill", "Simon", "Atanas",
		"Yana", "Kai", "Steven", "Archimedes", "Euklid", "Isaac", "Pythagoras", "Homer",
		"Bart", "Tux", "Gerd", "Garfield", "David", "Goliath", "Peter", "Alex", "Andi",
		"Nikolaus", "Xerxes", "Leonidas", "Siegfried", "Tom", "Ozzy", "Itchy", "Scratchy",
		"Odysseus", "Achilles", "Harald", "Gandalf", "Jesus", "Adolf",
		"Gott", "Satan", "Bartimaeus", "Nathaniel", "Kitty"
		};
	public static int totalThreads = 0;
	public int id;
	public int cycles;
	public int jobLength;
	public String name;
	

	iThread_35(int cycles, int jobLength) {
		this.id = iThread_35.totalThreads++;
		this.cycles = cycles;
		this.jobLength = jobLength;
		this.name = threadNames[(int)Math.floor(Math.random()*threadNames.length)];
	}

	public void run() {
		for (int j = 0; j < this.cycles; j++) {
			for (int i = 0; i < this.jobLength; i++) {
				calcPrimes(15000);
			}
			GraphicBufferThreadA1.threadBuffer.setBuffer(this.id, (int) ((100.0*j/this.cycles)));
		}
		GraphicBufferThreadA1.threadBuffer.setBuffer(this.id, 100);
	}
	
	public void calcPrimes(int n) {
		for (int i = 0; i < n; i++) {
			isPrime(i);
		}
	}
	
	/*
	* Returns true when number is a prime number
	*/
	public boolean isPrime(int number) {
		int root = (int) Math.ceil(Math.sqrt(number));
		if (number == 2) { return true; }
		if ((number & 1) == 0) { return false; }
		for (int div = 3; div <= root; div++) {
			if (number % div == 0) { return false; }
		}
		return true;
	}

}